package aigle.idm.diagraph.tp4.diagram_default_root.edit.commands;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest;

import aigle.idm.diagraph.tp4.Calculateur;
import aigle.idm.diagraph.tp4.Interface;
import aigle.idm.diagraph.tp4.Peripherique;

/**
 * @generated
 */
public class InterfaceReorientCommand extends EditElementCommand {

	/**
	 * @generated
	 */
	private final int reorientDirection;

	/**
	 * @generated
	 */
	private final EObject oldEnd;

	/**
	 * @generated
	 */
	private final EObject newEnd;

	/**
	 * @generated
	 */
	public InterfaceReorientCommand(ReorientRelationshipRequest request) {
		super(request.getLabel(), request.getRelationship(), request);
		reorientDirection = request.getDirection();
		oldEnd = request.getOldRelationshipEnd();
		newEnd = request.getNewRelationshipEnd();
	}

	/**
	 * @generated
	 */
	public boolean canExecute() {
		if (false == getElementToEdit() instanceof Interface) {
			return false;
		}
		if (reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) {
			return canReorientSource();
		}
		if (reorientDirection == ReorientRelationshipRequest.REORIENT_TARGET) {
			return canReorientTarget();
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected boolean canReorientSource() {
		if (!(oldEnd instanceof Calculateur && newEnd instanceof Calculateur)) {
			return false;
		}
		Peripherique target = getLink().get_peripherique();
		return aigle.idm.diagraph.tp4.diagram_default_root.edit.policies.Tp4BaseItemSemanticEditPolicy
				.getLinkConstraints().canExistInterface_3001(getLink(),
						getNewSource(), target);
	}

	/**
	 * @generated
	 */
	protected boolean canReorientTarget() {
		if (!(oldEnd instanceof Peripherique && newEnd instanceof Peripherique)) {
			return false;
		}
		if (!(getLink().eContainer() instanceof Calculateur)) {
			return false;
		}
		Calculateur source = (Calculateur) getLink().eContainer();
		return aigle.idm.diagraph.tp4.diagram_default_root.edit.policies.Tp4BaseItemSemanticEditPolicy
				.getLinkConstraints().canExistInterface_3001(getLink(), source,
						getNewTarget());
	}

	/**
	 * @generated
	 */
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
			IAdaptable info) throws ExecutionException {
		if (!canExecute()) {
			throw new ExecutionException(
					"Invalid arguments in reorient link command"); //$NON-NLS-1$
		}
		if (reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) {
			return reorientSource();
		}
		if (reorientDirection == ReorientRelationshipRequest.REORIENT_TARGET) {
			return reorientTarget();
		}
		throw new IllegalStateException();
	}

	/**
	 * @generated
	 */
	protected CommandResult reorientSource() throws ExecutionException {
		getOldSource().getInterfaces().remove(getLink());
		getNewSource().getInterfaces().add(getLink());
		return CommandResult.newOKCommandResult(getLink());
	}

	/**
	 * @generated
	 */
	protected CommandResult reorientTarget() throws ExecutionException {
		getLink().set_peripherique(getNewTarget());
		return CommandResult.newOKCommandResult(getLink());
	}

	/**
	 * @generated
	 */
	protected Interface getLink() {
		return (Interface) getElementToEdit();
	}

	/**
	 * @generated
	 */
	protected Calculateur getOldSource() {
		return (Calculateur) oldEnd;
	}

	/**
	 * @generated
	 */
	protected Calculateur getNewSource() {
		return (Calculateur) newEnd;
	}

	/**
	 * @generated
	 */
	protected Peripherique getOldTarget() {
		return (Peripherique) oldEnd;
	}

	/**
	 * @generated
	 */
	protected Peripherique getNewTarget() {
		return (Peripherique) newEnd;
	}
}
